import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item, ModalService } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { RequestService, GlobalService, AuthService } from '../../../../cores/services';
import { JoblService } from '../../services/job.service';
import { Recharge } from '../../interfaces/recharge.interfaces';
import { skipWhile, switchMap, map } from 'rxjs/operators';
import { ModalComponent } from './modal.component';
import { Permission } from '../../interfaces/permission.interfaces';

@Component({
    templateUrl: './department.component.html',
    styleUrls: ['./department.component.scss']
})
export class DepartmentComponent implements OnInit {

    theads = ['序号', '部门', '职位', '操作'];

    search = new SearchParams({ status: '', uid: '', orderSn: '', companyId: 0, departmentId: '' });
    id: number;
    list = new Array<any>();
    conpay = new Array<any>();
    banner: Recharge = { companyId: 0, };
    loading = false;
    companyId: 0;
    company = new Array<Item>();
    shops = new Array<any>();
    ids = new Array<any>();
    shopsone = new Array<any>();
    companyone = new Array<Item>();
    pagination = new Pagination();
    getlist = new Array<any>();
    permission = {};
    userInfo: any;
    getlista = new Array<any>();
    constructor(
        private request: RequestService,
        private activatedRoute: ActivatedRoute,
        private router: Router,
        public global: GlobalService,
        private confirm: ConfirmService,
        private joblService: JoblService,
        private toast: ToastService,
        private modal: ModalService,
        private auth: AuthService,
    ) {
        console.log(this.auth.user);
    }
    ngOnInit() {
        this.loadDatas();
    }
    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }
    doReset() {
        this.search.clean();
        this.doSearch();
        this.search.params.departmentId = '';
    }
    /**
     *职位列表
     */
    loadDatas() {
        this.loading = true;
        this.joblService.searchCrash(this.pagination, this.search).subscribe({
            next: res => {
                this.pagination.total = res.datas.total;
                this.list = res.datas.rows;
            },
            complete: () => this.loading = false
        });
    }
    /**
     * 删除职位
     * @param id
     */
    del(id: number) {
        this.confirm.danger('删除确认', `该职位下所有数据都将删除，您确认删除职位信息吗？`)
            .pipe(switchMap<void, ApiData>(() => this.joblService.del(id)))
            .subscribe(() => {
                console.log(id);
                this.list.splice(this.list.indexOf(id), 1);
                this.toast.success('删除成功', `成功删除职位信息`);
                this.loadDatas();
            });
    }
    /**
     * 保存职位
     * @param banner
     */
    showEditModal(banner?: Recharge) {
        banner = banner || { id: 0, positionName: '', companyId: 1, departmentId: '' };
        const modal = this.modal.create(ModalComponent, { center: true });
        modal.instance.banner = banner;
        modal.open().subscribe(() => {
            this.toast.success('保存成功', '成功保存职位');
            this.loadDatas();
        });
    }


}
